# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=zlib-ng
pkgver=2.1.8
pkgrel=0
pkgdesc="zlib replacement with optimizations for next generation systems"
url="https://github.com/zlib-ng/zlib-ng"
arch="all"
license="Zlib"
makedepends="cmake samurai"
checkdepends="gzip xxd zlib-dev"
subpackages="$pkgname-dev"
source="https://github.com/zlib-ng/zlib-ng/archive/$pkgver/zlib-ng-$pkgver.tar.gz"

# secfixes:
#   2.0.6-r0:
#     - CVE-2022-37434

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# WITH_UNALIGNED - unaligned access invokes undefined behaviour,
	#   see https://github.com/gentoo/gentoo/pull/17167 for more info.
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DWITH_DFLTCC_DEFLATE=ON \
		-DWITH_DFLTCC_INFLATE=ON \
		-DWITH_UNALIGNED=OFF \
		-DZLIB_ENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DZLIBNG_ENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DWITH_GTEST=OFF \
		$crossopts
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
062c5669791ec7f29e3bc17031c3c4dfec84053d38b39202d2fa91d61e54e6506fe7e086874880caf01a96e0052a69ecc6d749a5a9291d37fdb9d1037d7749e9  zlib-ng-2.1.8.tar.gz
"
